<template>
	<xn-form-container
		:title="formData.id ? '修改联系人' : '新增联系人'"
		:width="550"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="与欠款人关系" name="relationship">
				<a-select
					v-model:value="formData.relationship"
					placeholder="请选择与欠款人关系"
					:options="callRecipientOptions"
				/>
			</a-form-item>
			<a-form-item label="联系人姓名" name="liaisonName">
				<a-input
					:disabled="isDisabled"
					v-model:value="formData.liaisonName"
					placeholder="请输入联系人姓名"
					allow-clear
				/>
			</a-form-item>
			<a-form-item label="联系人电话" name="liaisonPhone">
				<a-input
					:disabled="isDisabled"
					v-model:value="formData.liaisonPhone"
					placeholder="请输入联系人电话"
					allow-clear
				/>
			</a-form-item>
			<a-form-item label="是否可用" name="phoneStatus">
				<a-select v-model:value="formData.phoneStatus" placeholder="请选择是否可用" :options="yesOrNoOptions" />
			</a-form-item>
			<a-form-item label="备注" name="remark">
				<a-input v-model:value="formData.remark" placeholder="请输入备注" allow-clear />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button class="xn-mr8" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="buttonLoading">保存</a-button>
		</template>
		<Icon-selector ref="iconSelectorRef" @iconCallBack="iconCallBack" />
	</xn-form-container>
</template>

<script setup>
	import { required } from '@/utils/formRules'
	import IconSelector from '@/components/Selector/iconSelector.vue'
	import tool from '@/utils/tool'
	import liaisonApi from '@/api/tjzy/liaison/liaisonApi'
	// 默认是关闭状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	const isDisabled = ref(false)
	const iconSelectorRef = ref()
	// 表单数据
	const formData = ref({
		phoneStatus: '1'
	})
	const buttonLoading = ref(false)

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		formData.value = record
		if (record.id !== undefined) {
			isDisabled.value = true
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		visible.value = false
		isDisabled.value = false
	}
	// 图标选择器回调
	const iconCallBack = (value) => {
		if (value) {
			formRef.value.clearValidate('icon')
		}
		formData.value.icon = value
	}
	const callRecipientOptions = tool.dictList('CallRecipient')
	const yesOrNoOptions = tool.dictList('YesOrNo')
	// 默认要校验的
	const formRules = {
		relationship: [required('请选择与欠款人关系')],
		liaisonName: [required('请输入联系人姓名')],
		liaisonPhone: [required('请输入联系人电话')],
		phoneStatus: [required('请选择是否可用')],
		remark: [required('请输入备注')]
	}

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				buttonLoading.value = true
				liaisonApi.addOrEditLiaison(formData.value).then(() => {
					onClose()
					emit('successful')
				})
			})
			.finally(() => {
				buttonLoading.value = false
			})
	}

	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>
